<!-- 随堂测试已经开始的页面 -->
<template>
	<view class="content">
		<Header :title="info.name" :isShowRight="false" @back='back' arrowColor='#000' textColor='#000'></Header>
		<view class="contents">
			<view class="centerBox">
				<!-- top -->
				<view class="top_box">
					<view class="system_text">
						<view>{{current+1}}/{{info.totalSubject}}</view>
						<text v-if="isSelect.subjectType == 0">判断题（{{isSelect.score}}分）</text>
						<text v-if="isSelect.subjectType == 1">单选题（{{isSelect.score}}分）</text>
						<text v-if="isSelect.subjectType == 2">多选题（{{isSelect.score}}分）</text>
					</view>
					<view>
						<u-count-down ref="countDown" :time="info.testTime * 60 * 1000" format="HH:mm:ss"
							:autoStart="true" @finish="onFinish" @change="onChange">
						</u-count-down>
					</view>
				</view>
				<!-- 题型 0：判断题，1：单选题，2：多选题 -->
				<view class="subject_list">
					<!-- 判断 -->
					<view class="subject_item" v-for="item,index in info.subjects" :key="index">
						<block v-if="item.checked">
							<view class="title">{{item.title}}</view>
							<!-- 选项 -->
							<view class="optioins_box">
								<view :class="['cell',cell.selectedFlag == 1?'cell_act':'']"
									v-for="cell,idx in item.options" :key="idx"
									@click="selectedAnswer(item.subjectType,index,idx)">
									<view class="opt_ico" v-show="item.subjectType ==2">
										<image v-if="cell.selectedFlag == 0" src="https://yzxk.oss-cn-hangzhou.aliyuncs.com/images/common/check.png"
											mode=""></image>
										<image v-else src="https://yzxk.oss-cn-hangzhou.aliyuncs.com/images/common/checked1.png" mode=""></image>
									</view>
									<view class="inner">
										<text
											v-if="item.subjectType != 0">{{`${cell.subjectCode}. ${cell.subjectContent}`}}</text>
										<text v-else>{{cell.subjectCode == 'A' ? '正确':'错误'}}</text>
									</view>

								</view>
							</view>
						</block>
					</view>
				</view>
				<!-- 下一题 -->
				<view class="flex align-center position_bottom">
					<image src="https://yzxk.oss-cn-hangzhou.aliyuncs.com/images/navgation.png" class="navgation" @click="sheetShow = true">
					</image>
					<view class="submit" v-if="current == info.totalSubject - 1" @click="goToScore">提交答案</view>
					<view class="nexts" v-else @click="gotoTest">下一题</view>
				</view>
			</view>
		</view>

		<!-- 测验题全部完成时的弹窗 -->
		<u-popup :show="completeShow" mode="center" :round="10">
			<view class="completePopup">
				<view class="message">
					<view class="first_message">确定提交答案？</view>
					<view class="">提交后不能修改</view>
				</view>
				<view class="chooseBtn">
					<view @click='completeShow = false'>我再想想</view>
					<view class="againSubmit" @click="confirmSubmit">确认提交</view>
				</view>
			</view>
		</u-popup>
		<!-- 测验题未全部完成时的弹窗 -->
		<u-popup :show="noCompleteShow" mode="center" :round="20">
			<view class="completePopup">
				<view class="message">
					<view class="first_message">您还未做答全部试题</view>
					<view class="">只有全部答题后才能提交哦~</view>
				</view>
				<view class="back" @click='backText'>返回继续答题</view>
			</view>
		</u-popup>
		<!-- 答题卡弹窗 -->
		<u-popup :show="sheetShow" :round="30">
			<view class="popupBox">
				<view class="title">
					<view>答题卡</view>
					<image src="https://yzxk.oss-cn-hangzhou.aliyuncs.com/images/close.png" @click="sheetShow = false"></image>
				</view>
				<view class="item_number allCenter">
					<view class="">{{info.testTotalSubject}}/{{info.totalSubject}}</view>
					<view class="flex">
						<view class="flex align-center">
							<view class="bg1"></view>
							<view class="finish">已做</view>
						</view>
						<view class="flex align-center">
							<view class="bg2"></view>
							<view class="unFinish">未做</view>
						</view>
					</view>
				</view>
				<!-- 循环得到答题数目 -->
				<view class="alltext">
					<view v-for='(item,index) in info.subjects' :key='index' class="item_select"
						@click="handlerNum(index)">
						<view :class="[item.memberRightKey ?'selected':'noSelect']">{{index+1}}</view>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		testDetail,
		testAdd
	} from '@/apis/apis.js'
	import Header from '@/components/c_header.vue'
	export default {
		components: {
			Header
		},
		data() {
			return {
				id: "",
				info: {}, // 测试信息 info.subjects// 题库
				current: 0, // 正在答题下标
				isSelect: {}, // 正在答题
				btnshow: true, //判断提交答案还是下一题
				completeShow: false, //测验题全部完成的控制弹窗
				noCompleteShow: false, //测验题未全部完成的控制弹窗
				sheetShow: false, //控制答题卡弹窗
				title: "",
				isios:false
			}
		},
		onLoad(options) {
			this.id = options.id
			let that = this
			uni.getSystemInfo({
				success: function(res) {
					if(res.platform == 'ios'){
						that.isios = true
					}
				}
			})
		},
		onShow() {
			this.init()
		},
		watch: {
			info: {
				handler(newVal, oldVal) {
					const val = newVal.subjects.filter(el => el.memberRightKey)
					this.info.testTotalSubject = val.length // 已完成题目数量
				},
				deep: true
			}
		},
		methods: {
			async init() {
				this.info = {}
				const res = await testDetail(this.id)
				console.log('初始信息', res)
				res.data.subjects.forEach(el => {
					el.checked = false
				})
				this.info = res.data
				this.checkedSub()
				// 设置开始时间
				let start = null
				if(this.isios){
					start = this.$timesFormat(new Date(), '/')
				}else{
					start = this.$timesFormat(new Date(), '-')
				}
				
				this.info.startTime = start
			},
			// 当前在答题目
			checkedSub() {
				this.info.subjects.forEach(el => el.checked = false)
				this.info.subjects[this.current].checked = true
				this.isSelect = this.info.subjects[this.current]
				// console.log(this.isSelect)
			},
			// 选中答案
			selectedAnswer(type, index, idx) {
				console.log(type, index, idx)
				// 题型 题目下标 选项下标
				if (type == 2) {
					// 多选 可以取消选择
					this.info.subjects[index].options[idx].selectedFlag = this.info.subjects[index].options[idx]
						.selectedFlag == 0 ?
						1 : 0
					this.fillAnswer(index)
				} else {
					// 判断和单选
					this.info.subjects[index].options.forEach(el => el.selectedFlag = 0)
					this.info.subjects[index].options[idx].selectedFlag = 1
					// 设置答案
					this.info.subjects[index].memberRightKey = this.info.subjects[index].options[idx].subjectCode
				}
				this.$forceUpdate()
			},
			// 多选填入答案
			fillAnswer(index) {
				// memberRightKey 用户选择答案 多个都逗号隔开
				const choose = this.info.subjects[index].options.filter(ele => ele.selectedFlag == 1)
				choose.sort(function(a, b) {
					return a.subjectCode.localeCompare(b.subjectCode)
				})
				let arr = []
				choose.forEach(el => {
					arr.push(el.subjectCode)
				})
				console.log(arr.join(','))
				this.info.subjects[index].memberRightKey = arr.join(',')
			},
			//点击下一题
			gotoTest() {
				if (this.current == this.info.totalSubject - 1) return
				this.current++
				this.checkedSub()
			},
			// 点击答题卡中选项
			handlerNum(i) {
				this.current = i
				this.checkedSub()
				this.sheetShow = false
			},
			// 倒计时变化
			onChange(e) {
				// console.log(e)
			},
			// 倒计时结束
			onFinish() {
				console.log('倒计时结束')
				uni.showModal({
					title: '测试时间结束',
					content: '测试时间到，是否重新答题',
					success: (res) => {
						if (res.confirm) {
							this.init()
						} else if (res.cancel) {
							uni.navigateBack()
						}
					}
				})
			},
			back() {
				uni.navigateBack()
			},
			//提交
			goToScore() {
				//先做判断测试题是否全部完成
				// console.log(this.info.testTotalSubject, this.info.totalSubject)
				if (this.info.testTotalSubject < this.info.totalSubject) {
					this.noCompleteShow = true
					return
				}
				this.completeShow = true
			},
			//试题未答完，返回继续做试题
			backText() {
				this.noCompleteShow = false
				this.sheetShow = true
			},
			//确认提交
			confirmSubmit() {
				// 结束时间
				this.calcTimeConsuming()
				testAdd(this.info).then(res => {
					console.log(res)
					if (res.code == 200) {
						this.completeShow = false
						this.reset()
						uni.redirectTo({
							url: `../textscore/testscore?id=${res.data.testMemberId}&courseId=${this.id}`
						})
					}
				})
			},
			reset() {
				this.info = {}
				this.current = 0
				this.init()
			},
			// 计算用时
			calcTimeConsuming() {
				let end = null
				if(this.isios){
					end = this.$timesFormat(new Date(), '/')
				}else{
					end = this.$timesFormat(new Date(), '-')
				}
				this.info.endTime = end
				
				const remain = new Date(end) - new Date(this.info.startTime)
				
				this.info.timeConsuming = remain / 1000
			},

		},

	}
</script>

<style scoped lang="scss">
	.flex {
		display: flex;
	}

	.align-center {
		align-items: center;
	}

	.justify-between {
		justify-content: space-between;
	}

	.allCenter {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.content {

		.contents {
			position: absolute;
			top: 140rpx;
			left: 0;
			right: 0;
			bottom: 0;
			background: url('https://yzxk.oss-cn-hangzhou.aliyuncs.com/images/testbg.png') no-repeat;
			background-size: cover;
		}

		.centerBox {
			position: absolute;
			top: 53rpx;
			left: 4%;
			right: 4%;
			bottom: 35rpx;
			box-shadow: 0 0 30rpx 0 rgba(60, 181, 254, 0.3) inset;
			border-radius: 20rpx;
			background-color: #fff;
			overflow: hidden;
			display: flex;
			flex-direction: column;

			// 题目时间
			.top_box {
				font-size: 30rpx;
				font-weight: 500;
				color: #818181;
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 62rpx 34rpx 66rpx 30rpx;

				.system_text {
					display: flex;
					align-items: center;
				}

			}

			.subject_list {
				width: 100%;
				flex: 1;
				padding-bottom: 100rpx;
				overflow: hidden;
				overflow-y: visible;

				// 题内容
				.subject_item {
					width: 90%;
					margin: 0 auto;

					.title {
						font-size: 30rpx;
						font-weight: 500;
						color: #202020;
						line-height: 40rpx;
						padding: 20rpx;
					}

					.optioins_box {
						font-size: 28rpx;
						color: #3A3E43;


						.cell {
							display: flex;
							align-items: center;
							height: 104rpx;
							margin-bottom: 20rpx;
							padding: 0 32rpx;
							border-radius: 10rpx;
							background: #F9F9F9;

							.opt_ico {
								width: 30rpx;
								height: 30rpx;
								margin-right: 24rpx;

								&>image {
									width: 100%;
									height: 100%;
								}
							}
						}

						.cell_act {
							background: #E5F7FF;
						}
					}
				}
			}

			.position_bottom {
				width: 100%;
				position: absolute;
				left: 0;
				bottom: 0;
				padding-bottom: 30rpx;
				background-color: #fff;
				display: flex;
				justify-content: center;
				align-items: center;

				.navgation {
					width: 36rpx;
					height: 35rpx;
					margin-right: 42rpx;
				}

				.nexts,
				.submit {
					width: 467rpx;
					height: 78rpx;
					background-color: #194FC9;
					border-radius: 10rpx;
					line-height: 78rpx;
					text-align: center;
					color: #fff;
					font-weight: 700;
				}
			}
		}
	}

	.completePopup {
		width: 550rpx;
		height: 278rpx;

		.message {
			width: 100%;
			height: 202rpx;
			border-bottom: 1rpx solid rgba(0, 0, 0, 0.2);
			color: #999999;
			font-size: 28rpx;
			display: flex;
			flex-direction: column;
			align-items: center;

			.first_message {
				color: #444444;
				font-size: 34rpx;
				font-weight: 700;
				margin: 48rpx 0 40rpx 0;
			}
		}

		.back {
			color: #194FC9;
			font-size: 28rpx;
			height: 78rpx;
			text-align: center;
			line-height: 78rpx;
		}

		.chooseBtn {
			width: 100%;

			display: flex;
			color: #666666;
			font-size: 28rpx;

			view {
				width: 50%;
				height: 78rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}

			.againSubmit {
				color: #194FC9;
				border-left: 1rpx solid rgba(0, 0, 0, 0.2);
			}
		}
	}

	.popupBox {
		width: 100%;
		height: 770rpx;

		.title {
			width: 100%;
			height: 110rpx;
			border: 1px solid rgba(0, 0, 0, 0.2);
			color: #111111;
			font-size: 38rpx;
			font-weight: bold;
			display: flex;
			justify-content: flex-end;
			align-items: center;
			padding-right: 40rpx;

			image {
				width: 30rpx;
				height: 30rpx;
				margin-left: 250rpx;
			}
		}

		.item_number {
			width: 100%;
			padding: 38rpx 30rpx 52rpx 30rpx;
			color: #666666;
			font-size: 30rpx;

			.bg1 {
				width: 26rpx;
				height: 26rpx;
				border-radius: 50%;
				background-color: #E5F7FF;
			}

			.bg2 {
				width: 24rpx;
				height: 24rpx;
				border-radius: 50%;
				border: 2rpx solid #BDBDBD;
			}

			.finish {
				font-size: 28rpx;
				padding-left: 12rpx;
				margin-right: 34rpx;
			}

			.unFinish {
				font-size: 28rpx;
				padding-left: 12rpx;
			}
		}

		.alltext {
			width: 100%;
			height: 505rpx;
			overflow: hidden;
			overflow-y: visible;
			padding: 0 40rpx;
			font-size: 34rpx;
			display: flex;
			flex-wrap: wrap;
			align-content: flex-start;

			.item_select {
				width: 14.28%;
				height: 70rpx;
				margin-bottom: 32rpx;
				display: flex;
				justify-content: center;

				.selected {
					width: 70rpx;
					height: 70rpx;
					line-height: 70rpx;
					text-align: center;
					border-radius: 50%;
					background-color: #E5F7FF;
					color: #194FC9;

				}

				.noSelect {
					width: 68rpx;
					height: 68rpx;
					line-height: 70rpx;
					text-align: center;
					border-radius: 50%;
					border: 2rpx solid #BDBDBD;
					color: #989898;

				}
			}




		}
	}
</style>
